<template>
  <!-- 新闻中心弹框 -->
  <popup :show="newsShow" title="新闻列表" @onClone="newsFun">
    <div class="newsDialog">
      <section class="newsCells">
        <ul>
          <template v-for="(item, index) in newsList">
            <li class="newscell" :key="index" @click="newsDetail(item)">
              <img :src="item.titlePic" alt="" />
              <div>
                <p>{{ item.title }}</p>
                <span>{{ item.time }}</span>
              </div>
            </li>
          </template>
        </ul>
      </section>
      <section class="pagination">
        <select v-model="pageSize" @change="selectChange">
          <option
            v-for="(option, idx) in options"
            :key="idx"
            :value="option.value"
          >
            {{ option.text }}
          </option>
        </select>
        <MoPaging
          :page-index="currentPage"
          :total="count"
          :page-size="pageSize"
          @pageChange="pageChange"
        >
        </MoPaging>
        <div class="pege">
          跳至
          <input type="tel" v-model="page" @blur="inputBlur" />
          页
        </div>
      </section>
    </div>
  </popup>
</template>

<script>
import popup from "../../popup.vue";
import MoPaging from "../mo-paging.vue";
import api from "@/util/api";
export default {
  components: {
    popup,
    MoPaging,
  },
  name: "newsDialog",
  data() {
    return {
      newsDialog: `<div>1111111111</div>`,
      newsList: [],
      page: 1,
      pageSize: 7, //每页显示10条数据
      currentPage: 1, //当前页码
      count: 100, //总记录数,
      options: [
        { text: "5条/页", value: 5 },
        { text: "7条/页", value: 7 },
        { text: "9条/页", value: 9 },
      ],
    };
  },
  computed: {
    newsShow: function () {
      return this.$store.state.newsShow;
    },
  },
  mounted(){
    this.getNewsList();
  },
  methods: {
    newsFun(data) {
      let that = this;
      that.$store.dispatch("changeFun", {
        str: "newsShow",
        cont: data,
      });
    },
    //从page组件传递过来的当前page
    pageChange(page) {
      this.currentPage = page;
      this.page = page;
      this.getNewsList();
    },
    // 每页条数改变
    selectChange() {
      this.getNewsList();
      // this.pageSize = this.selected;
    },
    // 当前页改变
    inputBlur() {
      // this.currentPage = this.page
      this.pageChange(+this.page);
    },
    // 新闻详情
    newsDetail(data) {
      let that = this;
      that.$store.dispatch("changeFun", {
        str: "newsDetailShow",  
        cont: true,
      });
      that.$store.dispatch("changeFun", {
        str: "newsDetails",  
        cont: data,
      });
    },
    // 获取最新的几条新闻
    getNewsList() {
      api.getNewsList(
        {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        },
        (res) => {
          console.log(res);
          this.newsList = res.data.list;
          this.count = res.data.total;
        }
      );
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.newsDialog {
  width: 1000px;
  height: 800px;
  overflow: auto;
  .buttonBox {
    width: 100%;
    height: auto;
    padding-bottom: 30px;
    display: flex;
    justify-content: space-between;
    ul {
      display: flex;
      justify-content: space-between;
      color: #fff;
      li {
        list-style-type: none;
        width: 100px;
        height: 43px;
        background-color: #135ba7;
        border-radius: 4px;
        text-align: center;
        line-height: 43px;
        margin-left: 12px;
      }
      li:nth-child(1) {
        margin: 0;
      }
      .active {
        background-color: #ff891d;
      }
    }
  }
  .newsCells {
      padding-top: 40px;
    ul {
      width: 100%;
      height: auto;
      .newscell {
        display: flex;
        margin-bottom: 15px;
        padding-bottom: 10px;
        border-bottom: 1px solid rgba(251, 251, 251, 0.5);
        cursor: pointer;
        img {
          width: 80px;
          height: 53px;
          margin-right: 30px;
        }
        div {
          font-size: 14px;
          font-family: PingFangSC, PingFangSC-Medium;
          font-weight: 500;
          color: #fff;
          p:hover {
            width: fit-content;
            border-bottom: 1px solid #fff;
          }
        }
        span {
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          color: rgba(251, 251, 251, 0.5);
          line-height: 34px;
        }
      }
    }
  }
  .pagination {
    width: fit-content;
    margin: 0 auto;
    padding-top: 10px;
    select {
      width: 95px;
      height: 40px;
      border: 1px solid #e6ecf3;
      border-radius: 2px;
      margin-left: 12px;
      padding-left: 12px;
      line-height: 40px;
      font-size: 15px;
      text-align: center;
      color: #fff;
      overflow: hidden;
      background-color: #00000000;
      option {
        color: #000;
      }
    }
    .pege {
      display: inline-block;
      color: #fff;
      font-size: 15px;
      input {
        width: 63px;
        height: 40px;
        color: #fff;
        border: 1px solid #e6ecf3;
        border-radius: 2px;
        padding-left: 6px;
        background-color: #00000000;
        box-sizing: border-box;
      }
    }
  }
}
</style>

